package groupby.persistence;


import groupby.model.GroupCriticism;
import groupby.model.GroupUserCriticism;
import groupby.util.Pages;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class GroupCriticismDaoImpl extends HibernateDaoSupport implements GroupCriticismDao {

	public int count(String id) {
		
		String hql = "select count(*) from GroupCriticism g where g.groupbyId="+id;
		  int cont=Integer.parseInt(this.getHibernateTemplate().find(hql).iterator().next()+"");
		  return cont;
	}

	/**
	 * 添加团购评论
	 */
	public boolean addGroupCriticism(GroupCriticism groupCriticism) {
		try
		{
		  this.getHibernateTemplate().save(groupCriticism);
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		return false;
	}

	/**
	 * 获得团购活动评论列表
	 */
	public List getGroupbyCriticismList(final int groupbyId,final Pages pages) {
		   List list=this.getHibernateTemplate().executeFind(new HibernateCallback(){

			public Object doInHibernate(Session session)throws HibernateException, SQLException {
				String hql="from GroupCriticism gc where gc.groupbyId="+groupbyId+" and gc.flag=null order by gc.datetime desc";
				int rowCount=getRowCount(hql);
				pages.setRowCount(rowCount);
				pages.setTotalPage(rowCount);
				pages.setPreNextPage();
				Query query =session.createQuery(hql);
				      query.setFirstResult((pages.getCurrentPage()-1)*pages.getPageSize());
				      query.setMaxResults(pages.getPageSize());
				return query.list();
			}
		   });
		return list;
	}
	public int getRowCount(final String hql)
    {
    	Integer rowCount=0;
    	rowCount=(Integer)this.getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)throws HibernateException, SQLException {
				return session.createQuery(hql).list().size();
			}
    	});
    	return rowCount;
    }

	/**
	 * 获得最新评论信息
	 */
	public List getNewGroupbyCriticism(final Pages pages) {
		 List list=this.getHibernateTemplate().executeFind(new HibernateCallback(){

				public Object doInHibernate(Session session)throws HibernateException, SQLException {
					String hql="from GroupCriticism gc where gc.flag=null order by gc.datetime desc ";
					int rowCount=getRowCount(hql);
					
					Query query =session.createQuery(hql);
					      query.setFirstResult((pages.getCurrentPage()-1)*pages.getPageSize());
					      query.setMaxResults(pages.getPageSize());
					return query.list();
				}
			   });
			return list;
	}
  /**
   * 获得所有品牌评论信息
   */
	public List getGroupbyUserCriticismList(final int groupbyUserId,final  Pages pages) {
		List list=this.getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session)throws HibernateException, SQLException {
				
				String hql="";
				if(groupbyUserId!=-1)
				{
					hql="from GroupUserCriticism gc where gc.groupbyUserId="+groupbyUserId+" order by gc.datetime desc";
				}else
				{
					hql="from GroupUserCriticism gc where gc.groupbyUserId in(select user.id from Users user where user.level=4) order by gc.datetime desc";
				}
				int rowCount=getRowCount(hql);
				pages.setRowCount(rowCount);
				pages.setTotalPage(rowCount);
				pages.setPreNextPage();
				Query query =session.createQuery(hql);
				      query.setFirstResult((pages.getCurrentPage()-1)*pages.getPageSize());
				      query.setMaxResults(pages.getPageSize());
				return query.list();
			}
		   });
		return list;
		
	}
   /**
    * add groupbyUserCriticism
    */
	public boolean addGroupUserCriticism(GroupUserCriticism groupCriticism) {
		
		try
		{
		  this.getHibernateTemplate().save(groupCriticism);
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		return false;
	}
/**
 * 获得相册图片回应列表
 */
public List criticismPhotoList(final Pages pages,final Integer id) {
	List list=(List)this.getHibernateTemplate().execute(new HibernateCallback(){

		public Object doInHibernate(Session session) throws HibernateException,SQLException {
			String hql="from GroupCriticism gc where gc.flag=4 and gc.groupbyId="+id+" order by gc.datetime desc";
			int rowCount=getRowCount(hql);
			pages.setRowCount(rowCount);
			pages.setTotalPage(rowCount);
			pages.setPreNextPage();
			Query query =session.createQuery(hql);
			      query.setFirstResult((pages.getCurrentPage()-1)*pages.getPageSize());
			      query.setMaxResults(pages.getPageSize());
			return query.list();
		}	
	});
	return list;
}
/**
 * 获得单个相册最新评论 
 */
public List newCriticismPhotoList(final Pages pages,final  String picId) {
  List list=(List)this.getHibernateTemplate().execute(new HibernateCallback(){

	public Object doInHibernate(Session session) throws HibernateException,SQLException {
		String hql="from GroupCriticism gc where gc.flag=4 and gc.groupbyId in(select p.id from Photoinfor p where p.picId in("+picId+")) order by gc.datetime desc";
		int rowCount=getRowCount(hql);
		pages.setRowCount(rowCount);
		pages.setTotalPage(rowCount);
		pages.setPreNextPage();
		Query query =session.createQuery(hql);
		      query.setFirstResult((pages.getCurrentPage()-1)*pages.getPageSize());
		      query.setMaxResults(pages.getPageSize());
		return query.list();
	}
	  
  });
	return list;
}
/**
 * 获得品牌评论信息
 */
public List criticismBrandList(final Pages pages,final  int brandId, final int id) {
	List list =(List)this.getHibernateTemplate().execute(new HibernateCallback(){

		public Object doInHibernate(Session session) throws HibernateException,SQLException {
			String hql="from GroupCriticism gc where gc.flag="+id+" and gc.groupbyId ="+brandId+" order by gc.datetime desc";
			int rowCount=getRowCount(hql);
			pages.setRowCount(rowCount);
			pages.setTotalPage(rowCount);
			pages.setPreNextPage();
			Query query =session.createQuery(hql);
			      query.setFirstResult((pages.getCurrentPage()-1)*pages.getPageSize());
			      query.setMaxResults(pages.getPageSize());
			return query.list();
		}
		
		
	});
	return list;
}
/**
 * 获得品牌的最新评论信息
 */
public List brandNewCriticismList(final Pages pages,final  String brandId) {
	List list =(List)this.getHibernateTemplate().execute(new HibernateCallback(){

		public Object doInHibernate(Session session) throws HibernateException,SQLException {
			String hql="from GroupCriticism gc where gc.flag=5 and gc.groupbyId in("+brandId+") order by gc.datetime desc";
			int rowCount=getRowCount(hql);
			pages.setRowCount(rowCount);
			pages.setTotalPage(rowCount);
			pages.setPreNextPage();
			Query query =session.createQuery(hql);
			      query.setFirstResult((pages.getCurrentPage()-1)*pages.getPageSize());
			      query.setMaxResults(pages.getPageSize());
			return query.list();
		}
		
		
	});
	return list;
	
}
/**
 * 获得话题评论信息
 */
public List criticismTopicList(final Pages pages,final  String topicId,final int id) {
	List list =(List)this.getHibernateTemplate().execute(new HibernateCallback(){

		public Object doInHibernate(Session session) throws HibernateException,SQLException {
			String hql="from GroupCriticism gc where gc.flag="+id+" and gc.groupbyId in("+topicId+") order by gc.datetime desc";
			int rowCount=getRowCount(hql);
			pages.setRowCount(rowCount);
			pages.setTotalPage(rowCount);
			pages.setPreNextPage();
			Query query =session.createQuery(hql);
			      query.setFirstResult((pages.getCurrentPage()-1)*pages.getPageSize());
			      query.setMaxResults(pages.getPageSize());
			return query.list();
		}
		
		
	});
	return list;
}
/**
 * 获得留言列表
 */
public List feedBack(final Pages pages,final  int userId) {
	List list=this.getHibernateTemplate().executeFind(new HibernateCallback(){
		public Object doInHibernate(Session session)throws HibernateException, SQLException {
			String hql="from GroupUserCriticism gc where gc.groupbyUserId="+userId+" order by gc.datetime desc";
			int rowCount=getRowCount(hql);
			pages.setRowCount(rowCount);
			pages.setTotalPage(rowCount);
			pages.setPreNextPage();
			Query query =session.createQuery(hql);
			      query.setFirstResult((pages.getCurrentPage()-1)*pages.getPageSize());
			      query.setMaxResults(pages.getPageSize());
			return query.list();
		}
	   });
	return list;
}
}
